package com.gemme.gemme.wode;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.MainActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.login.UseInfoBean;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.view.Topview;
import com.google.android.material.tabs.TabLayout;
import com.hyphenate.helpdesk.easeui.widget.ToastHelper;
import com.jaeger.library.StatusBarUtil;
import com.luck.picture.lib.tools.ToastUtils;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//侧滑删除地址
public class WoJuanActivity extends BaseActivity{
    int status = 0;
    boolean isclick = true;
    Double priceSum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_juan_wo);
        initTop();
        initView();
        initTab();
        initData();
    }



    AddressNewAdapter adapter;
    ArrayList<String> mDataList = new ArrayList<>();
    SwipeRecyclerView recyclerView;
    private void initView() {
       //
        recyclerView = findViewById(R.id.rvResult);

        mDataList.add("1");
        mDataList.add("2");
        mDataList.add("3");
        mDataList.add("4");
        priceSum = new Intent().getDoubleExtra("pricesum",0.0);
        Log.e("@@@",""+priceSum);
       /* DefaultItemDecoration mItemDecoration = new DefaultItemDecoration(ContextCompat.getColor
                (this, R.color.divider_color));
        recyclerView.addItemDecoration(mItemDecoration);*/
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mLayoutManager);
//        recyclerView.setLongPressDragEnabled(false); // 拖拽排序，默认关闭。
       /* adapter = new AddressNewAdapter();
        recyclerView.setAdapter(adapter);*/
//        adapter.notifyDataSetChanged(mDataList);


    }

    TabLayout tablayout;
    private void initTab() {
         String[] titles = new String[]{"可使用(0)","已使用(0)","已过期(0)"};
        tablayout = findViewById(R.id.tablayout);
        tablayout.addTab(tablayout.newTab().setTag(0));
        tablayout.addTab(tablayout.newTab().setTag(1));
        tablayout.addTab(tablayout.newTab().setTag(2));
        for(int i=0;i<titles.length;i++){
            tablayout.getTabAt(i).setText(titles[i]);
        }

        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                status = (Integer) tab.getTag();
                initData();
                System.out.println();
                recyclerView.setVisibility(View.GONE);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                System.out.println();
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                System.out.println();
            }
        });
    }


    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("我的折扣劵");
    }


    private void initData() {
//        http://192.168.1.2:9999/mall/shopdiscountcoupons/app/list?current=0&size=10&status=0
        String url = DataManager.BaseUrl_yanglinyu + "mall/shopdiscountcoupons/app/list?" +
                "current=" + "0" +
                "&size=" + "10" +
                "&status=" + status;

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put("Authorization",DataManager.getToken(mContext));
        DataManager.toServer3(mContext, url, DataManager.GET_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
                    DiscoutBean discoutBean = JSON.parseObject(response, DiscoutBean.class);
                    int total = discoutBean.getData().getTotal();
                    setTab(total);
                    List<DiscoutBean.DataBean.RecordsBean> records = discoutBean.getData().getRecords();
                    System.out.println();

                    recyclerView.setVisibility(View.VISIBLE);
                     adapter = new AddressNewAdapter(records);
                    recyclerView.setAdapter(adapter);
                }catch (Exception e){

                }
            }
        });

    }
/*
* ,"已使用","已过期"
* */
    private void setTab(int total) {
        try {
            switch (status){
                case 0:
                    tablayout.getTabAt(status).setText("可使用("+total+")");
                    break;
                case 1:
                    tablayout.getTabAt(status).setText("已使用("+total+")");
                    break;
                case 2:
                    tablayout.getTabAt(status).setText("已过期("+total+")");
                    break;
            }
        }catch (Exception e){

        }

       /* switch (status){
            case 0:

                break;
        }*/
//            tablayout.getTabAt(total).setText(titles[i]);

    }


    public class AddressNewAdapter extends RecyclerView.Adapter<AddressNewAdapter.ViewHolder>{


        private List<DiscoutBean.DataBean.RecordsBean> mDataList;
        public AddressNewAdapter(List<DiscoutBean.DataBean.RecordsBean> mDataList){
            this.mDataList = mDataList;
        }


        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
            //adapter_address
            View inflate = LayoutInflater.from(viewGroup.getContext()).
                    inflate(R.layout.wo_juan_item, null);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            inflate.setLayoutParams(layoutParams);
            return new AddressNewAdapter.ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            try {
                initStatue(holder);
                DiscoutBean.DataBean.RecordsBean recordsBean = mDataList.get(position);
                /**
                 * 0:折扣券 1：优惠券
                 */
                if(recordsBean.getType() == 0){
                    holder.tv_head.setText((int)recordsBean.getFaceValue() + "折");
                    holder.tv_type.setVisibility(View.GONE);
                }else {

                    holder.tv_head.setText(recordsBean.getCouponFaceValue() + "元");
                    holder.tv_type.setVisibility(View.VISIBLE);
                    if(recordsBean.getCouponType()==0){
                        holder.tv_type.setText("满"+recordsBean.getMeetPrice()+"减"+recordsBean.getCouponFaceValue());
                        if(new BigDecimal(priceSum).compareTo(new BigDecimal(recordsBean.getMeetPrice()))==-1) {
                            holder.tv_join.setBackgroundResource(R.drawable.shape_corn14_gray);
                            isclick = false;
                        }else {
                            holder.tv_join.setBackgroundResource(R.drawable.shape_corn14_red);
                            isclick = true;
                        }
                    }else {
                        holder.tv_type.setText("无门槛券");
                    }
                }
                holder.tv_name.setText(recordsBean.getRemarks());
                holder.tv_time.setText("有效期至:"+recordsBean.getEndDate().substring(0,10));
                System.out.println("");

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(recordsBean.getType() ==0){
                            isclick = true;
                        }else {
                           if (recordsBean.getCouponType()==0){
                               if(new BigDecimal(priceSum).compareTo(new BigDecimal(recordsBean.getMeetPrice()))==-1){
                                   isclick = false;
                               }else {
                                   isclick = true;
                               }
                           }else {
                               isclick = true;
                           }
                        }
                        if(isclick){
                            Intent intent = new Intent();
                            intent.putExtra("data",JSON.toJSONString(recordsBean));
                            setResult(1000,intent);
                            finish();
                        }else {
                            Toast.makeText(mContext, "不可用", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }catch (Exception e){

            }
        }

        private void initStatue(ViewHolder holder) {
            switch (status){
                case 0:
                    holder.tv_head.setBackgroundResource(R.drawable.pc_discount_no);
                    holder.tv_head.setTextColor(Color.parseColor("#FFFFFFFF"));
                    holder.tv_name.setTextColor(Color.parseColor("#FF333333"));
                    holder.tv_time.setTextColor(Color.parseColor("#FF333333"));
                    holder.tv_join.setBackgroundResource(R.drawable.shape_corn14_red);
                    break;

               /* case 1:

                    break;

                case 2:

                    break;*/

                default:
                    holder.tv_head.setBackgroundResource(R.drawable.pc_discount_no);
                    holder.tv_head.setTextColor(Color.parseColor("#FF333333"));
                    holder.tv_name.setTextColor(Color.parseColor("#FF333333"));
                    holder.tv_time.setTextColor(Color.parseColor("#FF333333"));
                    holder.tv_join.setBackgroundResource(R.drawable.shape_corn14_gray);
                    break;
            }
        }

        @Override
        public int getItemCount() {
            return mDataList.size();
        }


        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_head;
            TextView tv_name;
            TextView tv_time;
            TextView tv_join;
            TextView tv_type;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                tv_type = itemView.findViewById(R.id.tv_type);
                tv_head = itemView.findViewById(R.id.tv_head);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_time = itemView.findViewById(R.id.tv_time);
                tv_join = itemView.findViewById(R.id.tv_join);
            }
        }
    }

}